<?php
/*
Bitsand - a web-based booking system for LRP events
Copyright (C) 2006 - 2008 The Bitsand Project (http://bitsand.googlecode.com/)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

//Do not check that user is logged in
$bLoginCheck = False;
//Initialise $CSS_PREFIX
$CSS_PREFIX = '../';

include ($CSS_PREFIX . 'inc/inc_head_db.php');
include ($CSS_PREFIX . 'inc/inc_head_html.php');
//Report all errors except E_NOTICE
error_reporting (E_ALL ^ E_NOTICE);
?>

<h1>Update Database</h1>

<p>
This page will update an existing Bitsand database (from Bitsand v3.1) to work with the latest version of Bitsand (version x). It will report progress as it goes. Note that, in order for this to work, the user defined in the configuration file must have permission to CREATE, ALTER and DROP tables in the specified database. To use it, enter the value of CRYPT_KEY from the configuration file and click &quot;Update&quot;.
</p>

<form action = "update_db.php" method = "post">
<p>
Value of CRYPT_KEY in configuration file: <input name = "txtKey" type = "password"><br>
<input type = "submit" value = "Update" name = "btnSubmit">
</p>
</form>

<p>
<?php
if ($_POST ['btnSubmit'] != '' && $_POST ['txtKey'] == CRYPT_KEY && CheckReferrer ('update_db.php')) {
	$db_prefix = DB_PREFIX;

	//Modify characters table
	echo "Modifying table: {$db_prefix}characters<br>\n";
	if (ba_db_query ($link, "ALTER TABLE `{$db_prefix}characters` ADD COLUMN `chPreferredName` text") === False)
		echo "<span class = 'sans-warn'>Error modifying table {$db_prefix}characters</span><br>\n";
	if (ba_db_query ($link, "ALTER TABLE `{$db_prefix}characters` ADD COLUMN `chAncestorSel` text") === False)
		echo "<span class = 'sans-warn'>Error modifying table {$db_prefix}characters</span><br>\n";
	echo "Table modified: {$db_prefix}characters<br>\n";

	//Modify players table
	echo "Modifying table: {$db_prefix}players<br>\n";
	if (ba_db_query ($link, "ALTER TABLE `{$db_prefix}players` ADD `plLastLogin` DATE NULL") === False)
		echo "<span class = 'sans-warn'>Error modifying table {$db_prefix}players</span><br>\n";
	//Set LastLogin date of existing players to today
	if (ba_db_query ($link, "UPDATE `{$db_prefix}players` SET `plLastLogin` = CURDATE()") === False)
		echo "<span class = 'sans-warn'>Error modifying table {$db_prefix}players</span><br>\n";
	echo "Table modified: {$db_prefix}players<br>\n";

	//Modify Bookings table
	echo "Modifying table: {$db_prefix}bookings<br>\n";
	if (ba_db_query ($link, "ALTER TABLE `{$db_prefix}bookings` ADD COLUMN `bkAmountPaid` decimal(5,2)") === False)
		echo "<span class = 'sans-warn'>Error modifying table {$db_prefix}bookings</span><br>\n";
	if (ba_db_query ($link, "ALTER TABLE `{$db_prefix}bookings` ADD COLUMN `bkAmountExpected` decimal(5,2)") === False)
		echo "<span class = 'sans-warn'>Error modifying table {$db_prefix}bookings</span><br>\n";
	echo "Table modified: {$db_prefix}bookings<br>\n";

	//Create Ancestors table
	echo "Creating table: {$dbprefix}ancestors<br>\n";
	if (ba_db_query ($link, "create table `{$db_prefix}ancestors` (`anID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, `anName` TEXT)") === False)
		echo "<span class = 'sans-warn'>Error creating table {$db_prefix}ancestors</span><br>\n";
	echo "Table created: {$db_prefix}ancestors<br>\n";

	echo "Inserting default record: {$dbprefix}ancestors<br>\n";
	if (ba_db_query ($link, "insert into `{$db_prefix}ancestors` (`anName`) VALUES ('Other (enter name below)')") === False)
		echo "<span class = 'sans-warn'>Error inserting default record into {$db_prefix}ancestors</span><br>\n";
	echo "Record inserted: {$db_prefix}ancestors<br>\n";

	function setBoolDefault ($bPassedValue)
	{
		if ($bPassedValue) {return '1';}
		else {return '0';}
	}

	function setZeroIfNull ($oPassedValue)
	{
		if (!is_null($oPassedValue)) {return $oPassedValue;}
		else {return '0';}
	}

	echo "Populating Config settings table<br>\n";
	//Create the config table
	$db_prefix = DB_PREFIX;


	$createConfigTableScript =	"CREATE TABLE IF NOT EXISTS {$db_prefix}config (`cnID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY,`cnName` TEXT,`cnPLAYER_SPACES` SMALLINT UNSIGNED,`cnMONSTER_SPACES` SMALLINT UNSIGNED,`cnSTAFF_SPACES` SMALLINT UNSIGNED,`cnTOTAL_SPACES` SMALLINT UNSIGNED,`cnPLAYER_BUNKS` SMALLINT UNSIGNED,`cnMONSTER_BUNKS` SMALLINT UNSIGNED,`cnSTAFF_BUNKS` SMALLINT UNSIGNED,`cnTOTAL_BUNKS` SMALLINT UNSIGNED,`cnANNOUNCEMENT_MESSAGE` TEXT,`cnDISCLAIMER_TEXT` TEXT,`cnEVENT_CONTACT_NAME` TEXT,`cnEVENT_CONTACT_MAIL` TEXT,`cnTECH_CONTACT_NAME` TEXT,`cnTECH_CONTACT_MAIL` TEXT,`cnTITLE` TEXT,`cnEVENT_NAME` TEXT,`cnSYSTEM_NAME` TEXT,`cnBOOKING_FORM_FILE_NAME` TEXT,`cnBOOKING_LIST_IF_LOGGED_IN` BOOL,`cnALLOW_MONSTER_BOOKINGS` BOOL,`cnLOCATIONS_LABEL` TEXT,`cnLIST_GROUPS_LABEL` TEXT,`cnANCESTOR_DROPDOWN` BOOL,`cnDEFAULT_FACTION` TEXT,`cnNON_DEFAULT_FACTION_NOTES` BOOL,`cnIC_NOTES_TEXT` TEXT,`cnLOGIN_TIMEOUT` SMALLINT UNSIGNED,`cnLOGIN_TRIES` SMALLINT UNSIGNED,`cnMIN_PASS_LEN` SMALLINT UNSIGNED,`cnSEND_PASSWORD` BOOL,`cnUSE_PAY_PAL` BOOL,`cnPAYPAL_EMAIL` TEXT,`cnPAYPAL_PLAYER_1` TEXT,`cnPAYPAL_PLAYER_2` TEXT,`cnPAYPAL_PLAYER_3` TEXT,`cnPAYPAL_PLAYER_4` TEXT,`cnPAYPAL_MONSTER_1` TEXT,`cnPAYPAL_MONSTER_2` TEXT,`cnPAYPAL_MONSTER_3` TEXT,`cnPAYPAL_MONSTER_4` TEXT,`cnPAYPAL_STAFF_1` TEXT,`cnPAYPAL_STAFF_2` TEXT,`cnPAYPAL_STAFF_3` TEXT,`cnPAYPAL_STAFF_4` TEXT,`cnPAYPAL_AMOUNT_P1` DECIMAL(6,2) NULL,`cnPAYPAL_AMOUNT_P2` DECIMAL(6,2) NULL,`cnPAYPAL_AMOUNT_P3` DECIMAL(6,2) NULL,`cnPAYPAL_AMOUNT_P4` DECIMAL(6,2) NULL, `cnPAYPAL_AMOUNT_M1` DECIMAL(6,2) NULL,`cnPAYPAL_AMOUNT_M2` DECIMAL(6,2) NULL,`cnPAYPAL_AMOUNT_M3` DECIMAL(6,2) NULL,`cnPAYPAL_AMOUNT_M4` DECIMAL(6,2) NULL,`cnPAYPAL_AMOUNT_S1` DECIMAL(6,2) NULL,`cnPAYPAL_AMOUNT_S2` DECIMAL(6,2) NULL,`cnPAYPAL_AMOUNT_S3` DECIMAL(6,2) NULL,`cnPAYPAL_AMOUNT_S4` DECIMAL(6,2) NULL, `cnNPC_LABEL` TEXT, `cnPAYPAL_AUTO_MARK_PAID` BOOL, `cnAUTO_ASSIGN_BUNKS` BOOL)";
	if (ba_db_query ($link, $createConfigTableScript) === False)
		echo "<span class = 'sans-warn'>Error creating config table</span><br>\n";


	//Clear the config table, and insert the default values
	if (ba_db_query ($link, "DELETE FROM `{$db_prefix}config`") === False)
		echo "<span class = 'sans-warn'>Error clearing config table</span><br>\n";

	if (ba_db_query ($link, "INSERT INTO `{$db_prefix}config` (cnName) VALUES ('Default')") === False)
		echo "<span class = 'sans-warn'>Error inserting default config row</span><br>\n";

	//Need to deal with quotes in strings
	$updateQuery = "UPDATE `{$db_prefix}config` SET ";
	$updateQuery.= "cnPLAYER_SPACES = ".ba_db_real_escape_string($link,setZeroIfNull(PLAYER_SPACES)).", ";
	$updateQuery.= "cnMONSTER_SPACES = ".ba_db_real_escape_string($link,setZeroIfNull(MONSTER_SPACES)).", ";
	$updateQuery.= "cnSTAFF_SPACES = ".ba_db_real_escape_string($link,setZeroIfNull(STAFF_SPACES)).", ";
	$updateQuery.= "cnTOTAL_SPACES = ".ba_db_real_escape_string($link,setZeroIfNull(TOTAL_SPACES)).", ";
	$updateQuery.= "cnANNOUNCEMENT_MESSAGE = '".ba_db_real_escape_string($link,ANNOUNCEMENT_MESSAGE)."', ";
	$updateQuery.= "cnDISCLAIMER_TEXT = '".ba_db_real_escape_string($link,DISCLAIMER_TEXT)."', ";
	$updateQuery.= "cnEVENT_CONTACT_NAME = '".ba_db_real_escape_string($link,EVENT_CONTACT_NAME)."', ";
	$updateQuery.= "cnEVENT_CONTACT_MAIL = '".ba_db_real_escape_string($link,EVENT_CONTACT_MAIL)."', ";
	$updateQuery.= "cnTECH_CONTACT_NAME = '".ba_db_real_escape_string($link,TECH_CONTACT_NAME)."', ";
	$updateQuery.= "cnTECH_CONTACT_MAIL = '".ba_db_real_escape_string($link,TECH_CONTACT_MAIL)."', ";
	$updateQuery.= "cnTITLE = '".ba_db_real_escape_string($link,TITLE)."', ";
	$updateQuery.= "cnEVENT_NAME = '".ba_db_real_escape_string($link,EVENT_NAME)."', ";
	$updateQuery.= "cnSYSTEM_NAME = '".ba_db_real_escape_string($link,SYSTEM_NAME)."', ";
	$updateQuery.= "cnBOOKING_FORM_FILE_NAME = '".ba_db_real_escape_string($link,BOOKING_FORM_FILE_NAME)."', ";
	$updateQuery.= "cnBOOKING_LIST_IF_LOGGED_IN = ".setBoolDefault(BOOKING_LIST_IF_LOGGED_IN).", ";
	$updateQuery.= "cnALLOW_MONSTER_BOOKINGS = ".setBoolDefault(ALLOW_MONSTER_BOOKINGS).", ";
	$updateQuery.= "cnLIST_GROUPS_LABEL = '".ba_db_real_escape_string($link,LIST_GROUPS_LABEL)."', ";
	$updateQuery.= "cnLOCATIONS_LABEL = '".ba_db_real_escape_string($link,LOCATIONS_LABEL)."', ";
	$updateQuery.= "cnANCESTOR_DROPDOWN = ".setBoolDefault(ANCESTOR_DROPDOWN).", ";
	$updateQuery.= "cnDEFAULT_FACTION = '".ba_db_real_escape_string($link,DEFAULT_FACTION)."', ";
	$updateQuery.= "cnNON_DEFAULT_FACTION_NOTES = ".setBoolDefault(NON_DEFAULT_FACTION_NOTES).", ";
	$updateQuery.= "cnIC_NOTES_TEXT = '".ba_db_real_escape_string($link,IC_NOTES_TEXT)."', ";
	$updateQuery.= "cnLOGIN_TIMEOUT = ".ba_db_real_escape_string($link,setZeroIfNull(LOGIN_TIMEOUT)).", ";
	$updateQuery.= "cnLOGIN_TRIES = ".ba_db_real_escape_string($link,setZeroIfNull(LOGIN_TRIES)).", ";
	$updateQuery.= "cnMIN_PASS_LEN = ".ba_db_real_escape_string($link,setZeroIfNull(MIN_PASS_LEN)).", ";
	$updateQuery.= "cnSEND_PASSWORD = ".setBoolDefault(SEND_PASSWORD).", ";
	$updateQuery.= "cnUSE_PAY_PAL = ".setBoolDefault(USE_PAY_PAL).", ";
	$updateQuery.= "cnPAYPAL_EMAIL = '".ba_db_real_escape_string($link,PAYPAL_EMAIL)."', ";
	$updateQuery.= "cnPAYPAL_PLAYER_1 = '".ba_db_real_escape_string($link,PAYPAL_PLAYER_1)."', ";
	$updateQuery.= "cnPAYPAL_PLAYER_2 = '".ba_db_real_escape_string($link,PAYPAL_PLAYER_2)."', ";
	$updateQuery.= "cnPAYPAL_PLAYER_3 = 'Player booking, no meal ticket, and a bunk', ";
	$updateQuery.= "cnPAYPAL_PLAYER_4 = 'Player booking, meal ticket and no bunk', ";
	$updateQuery.= "cnPAYPAL_MONSTER_1 = '".ba_db_real_escape_string($link,PAYPAL_MONSTER_1)."', ";
	$updateQuery.= "cnPAYPAL_MONSTER_2 = '".ba_db_real_escape_string($link,PAYPAL_MONSTER_2)."', ";
	$updateQuery.= "cnPAYPAL_MONSTER_3 = 'Monster booking, no meal ticket, and a bunk', ";
	$updateQuery.= "cnPAYPAL_MONSTER_4 = 'Monster booking, meal ticket and no bunk', ";
	$updateQuery.= "cnPAYPAL_STAFF_1 = '".ba_db_real_escape_string($link,PAYPAL_STAFF_1)."', ";
	$updateQuery.= "cnPAYPAL_STAFF_2 = '".ba_db_real_escape_string($link,PAYPAL_STAFF_2)."', ";
	$updateQuery.= "cnPAYPAL_STAFF_3 = 'Staff booking, no meal ticket, and a bunk', ";
	$updateQuery.= "cnPAYPAL_STAFF_4 = 'Staff booking, meal ticket and no bunk', ";
	if (PAYPAL_AMOUNT_P1 == '')
		$updateQuery.= "cnPAYPAL_AMOUNT_P1 = null, ";
	else
		$updateQuery.= "cnPAYPAL_AMOUNT_P1 = ".ba_db_real_escape_string($link,PAYPAL_AMOUNT_P1).", ";
	if (PAYPAL_AMOUNT_P2 == '')
		$updateQuery.= "cnPAYPAL_AMOUNT_P2 = null, ";
	else
		$updateQuery.= "cnPAYPAL_AMOUNT_P2 = ".ba_db_real_escape_string($link,PAYPAL_AMOUNT_P2).", ";
	if (PAYPAL_AMOUNT_M1 == '')
		$updateQuery.= "cnPAYPAL_AMOUNT_M1 = null, ";
	else
		$updateQuery.= "cnPAYPAL_AMOUNT_M1 = ".ba_db_real_escape_string($link,PAYPAL_AMOUNT_M1).", ";
	if (PAYPAL_AMOUNT_M2 == '')
		$updateQuery.= "cnPAYPAL_AMOUNT_M2 = null, ";
	else
		$updateQuery.= "cnPAYPAL_AMOUNT_M2 = ".ba_db_real_escape_string($link,PAYPAL_AMOUNT_M2).", ";
	if (PAYPAL_AMOUNT_S1 == '')
		$updateQuery.= "cnPAYPAL_AMOUNT_S1 = null, ";
	else
		$updateQuery.= "cnPAYPAL_AMOUNT_S1 = ".ba_db_real_escape_string($link,PAYPAL_AMOUNT_S1).", ";
	if (PAYPAL_AMOUNT_S2 == '')
		$updateQuery.= "cnPAYPAL_AMOUNT_S2 = null,";
	else
		$updateQuery.= "cnPAYPAL_AMOUNT_S2 = ".ba_db_real_escape_string($link,PAYPAL_AMOUNT_S2).", ";

	//From here we can add new config values that were never in the text file
	$updateQuery.= "cnNPC_LABEL = 'Are you an NPC?:', ";
	$updateQuery.= "cnPAYPAL_AUTO_MARK_PAID = 1, ";
	$updateQuery.= "cnAUTO_ASSIGN_BUNKS = 0";

	if (ba_db_query ($link, $updateQuery) === False)
		echo "<span class = 'sans-warn'>Error setting up default values in config table</span><br>\n";

	echo "Config settings populated<br/>\n";
	echo "<b>Database updated</b><br>\n";

	echo "You should alter your config file so that it only contains the following settings:<br/>\n";
	echo "<ol>
		<li>PID_PREFIX</li>
		<li>ROOT_USER_ID</li>
		<li>DB_TYPE</li>
		<li>DB_HOST</li>
		<li>DB_NAME</li>
		<li>DB_PREFIX</li>
		<li>DB_USER</li>
		<li>DB_PASS</li>
		<li>CRYPT_KEY</li>
		<li>PW_SALT</li>
		<li>OLD_PW_SALT</li>
		<li>SYSTEM_URL</li>
		<li>DEBUG_MODE</li>
		<li>MAINTENANCE_MODE</li>
		<li>WARNING_LOG</li>
		<li>ERROR_LOG</li>
		<li>NUM_GUILDS</li>
		<li>MAX_CHAR_PTS</li>
		<li>MAX_NPC_PTS</li>
		<li>MAX_OSPS</li>
		<li>RPG_NAME</li>
	</ol>
	<p>You can use your current config file to generate the text for a new file that you can copy and paste over the old inc_config.php. WARNING this will display your passwords and encryption keys in plain text.
	<form action='create_config_text.php' method='post'>
	Enter encryption key to proceed: <input type='password' name='txtCryptKey' />
	<input type=submit name='btnSubmit' />
	</form>
	</p>";
}

elseif ($_POST ['btnSubmit'] != '' && $_POST ['txtKey'] != CRYPT_KEY)
	echo "<span class = 'sans-warn'>Wrong value entered for CRYPT_KEY</span>";
?>
</p>

<p><a href = "./">Installation Tests &amp; Tools</a></p>

<?php
include ('../inc/inc_foot.php');
?>
